#!/bin/sh
#
# Bump a port PORTREVISION

if [ $# -eq 1 -a -d "$1" ]; then
	cd $1
fi

if [ ! -f "Makefile" ]; then
	echo "*** Makefile not found! (PWD=$PWD)" >&2
	exit 1
fi

PORTREVISION=`make -V PORTREVISION`

ret=1
cp "Makefile" "Makefile.bak"

if grep -q "^PORTREVISION.*${PORTREVISION}\$" "Makefile"; then
	sed -e "s|^\\(PORTREVISION.*\\)${PORTREVISION}\$|\\1$((${PORTREVISION}+1))|" < "Makefile.bak" > "Makefile"
	ret=$?
else
	if grep -q "^PORTVERSION" "Makefile"; then
		sed  -e "/^PORTVERSION/a\\
		PORTREVISION=	$((${PORTREVISION}+1))" < "Makefile.bak" > "Makefile"
		ret=$?
	else
		if grep -q "^PORTNAME" "Makefile"; then
			sed  -e "/^PORTNAME/a\\
			PORTREVISION=	$((${PORTREVISION}+1))" < "Makefile.bak" > "Makefile"
			ret=$?
		fi
	fi
fi

if [ $ret -eq 0 ]; then
	rm "Makefile.bak"
else
	echo "*** Don't know where to add PORTREVISION!" >&2
	mv "Makefile.bak" "Makefile"
fi

exit $ret
